<? include ("../init.php") ?>
<?
  $aid = $_POST['aid'];
  $cat = $_POST['cat'];
  $collection = $_POST['collection'];
  $name = addslashes($_POST['name']);
  
  $style_number = addslashes(strtolower($_POST['style_number']));
  $product_type = addslashes(strtolower($_POST['product_type']));
  $metal = addslashes($_POST['metal']);
  $lenght_inch = addslashes($_POST['lenght_inch']);
  $lenght_mm = addslashes($_POST['lenght_mm']);
  $width_mm = addslashes($_POST['width_mm']);
  
  $detail = addslashes($_POST['detail']);
  $active = ($_POST['active']);
  $user_allow = ($_POST['user_allow']);
  
  $allow = 3;
  foreach($user_allow as $v){
    $allow -= $v;
  }
  
  $sql = "UPDATE hs_product SET c_id='$cat',pc_id='$collection',p_name='$name',p_detail='$detail',p_active='$active',user_allow='$allow',p_style_number='$style_number',p_type='$product_type',p_metal='$metal',p_lenght_inch='$lenght_inch',p_lenght_mm='$lenght_mm',p_width_mm='$width_mm' WHERE  p_id=$aid";

  $db->execute($sql);
  
  // ---------- UPLOADS Image ----------
  $handle = new Upload($_FILES['image']);
  
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->image_resize = true;
        $handle->image_ratio_fill = true;
        $handle->image_x = 600;
        $handle->image_y = 600;
        $handle->Process(APPPATH.'/images/products/product/'.$aid);
        
        if ($handle->processed)
        {
           $sql = "UPDATE hs_product SET p_img='$handle->file_dst_name' WHERE p_id=$aid";
           if(!mysql_query($sql))
           {
                $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
                unlink(APPPATH."/images/products/product/$aid/".$handle->file_dst_name);
           }else{
            if(trim($_POST['oldimg'])!='' AND file_exists(APPPATH.'/images/products/product/'.$aid.'/'.$_POST['oldimg'])){
              @unlink(APPPATH.'/images/products/product/'.$aid.'/'.$_POST['oldimg']);
            }
           }
        }
        else
        {
            $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
        }
        
        $handle->clean();
    }
    else
    {
        $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
    }
  }
  
  //$sql = "UPDATE product SET product_img='$name' WHERE aid=$aid";
  //$db->execute($sql);
  
  header("Location:products.php");
?>